#include "Cue.h"

namespace coolpool
{
	Cue::Cue(Ball * ball, double min_dist, double max_dist, double spring_stiffnes, GLuint texture)
	{
		ball_ = ball;
		min_dist_ = min_dist;
		max_dist_ = max_dist;
		position_ = math_tools::Vector3D(0, 0, -min_dist);
		spring_stiffnes_ = spring_stiffnes;
		texture_ = texture;
	}

	Cue::~Cue()
	{

	}

	void Cue::render()
	{
		// Compute the ends of the triangle.
		math_tools::Vector3D abs_position = ball_->getCenterOfMassLocation() + position_;
		math_tools::Vector3D close_side = ball_->getCenterOfMassLocation() + position_ * ((min_dist_ / 2) / position_.mag());
		math_tools::Vector3D normal = (position_ * math_tools::Vector3D(0.0, 1.0, 0.0)).unit();
		normal *= ball_->getRadius() / 2;
		math_tools::Vector3D v1 = close_side - normal;
		math_tools::Vector3D v2 = close_side + normal;

		glColor3d(1.0, 0.0, 0.0);
		glEnable(GL_TEXTURE_2D);
		glBindTexture(GL_TEXTURE_2D, texture_);

		glPushMatrix();
		glBegin(GL_TRIANGLES);
		glTexCoord2f(1.0f, 0.0f);
		glVertex3f(v1.getX(), v1.getY(), v1.getZ());
		glTexCoord2f(1.0f, 1.0f);
		glVertex3f(v2.getX(), v2.getY(), v2.getZ());
		glTexCoord2f(0.0f, 1.0f);
		glVertex3f(abs_position.getX(), abs_position.getY(), abs_position.getZ());
		glEnd();
		glPopMatrix();
	}
}
